namespace :htaccess do
  desc "Pull down the .htaccess file"
  task :pull do
    file = ".htaccess"
    
    remote_file = File.join(release_path, file)
    
    on roles(:app) do |server|
      unless test("[ -f #{remote_file} ]")
        error "No #{file} file found on #{server.user}@#{server.hostname}"
        
        next
      end
      
      if File.file? file
        local_sha256sum = Digest::SHA256.hexdigest(File.read(file))
        remote_sha256sum = capture("sha256sum #{remote_file}").split(' ').first
        
        if local_sha256sum == remote_sha256sum
          info "No changes detected in #{file} file on #{server.user}@#{server.hostname}"
          
          next
        end
        
        unless fetch(:confirm_pull_htaccess)
          set :confirm_pull_htaccess, ask("confirmation for local #{file} file overwrite", "Y/n")
        end
        
        next unless true == fetch(:confirm_pull_htaccess) or ["y", "yes"].include? fetch(:confirm_pull_htaccess).downcase
      end
      
      info "Pulling #{file} file from #{server.user}@#{server.hostname}"
      
      download! remote_file, file
      
      break
    end
  end
end
